//
//  JSQShowZoomImageScrollView.m
//  Conversation
//
//  Created by C_HAO on 15/10/16.
//  Copyright © 2015年 CHAOO. All rights reserved.
//

#import "JSQShowZoomImageScrollView.h"

#define ScreenWidth      [UIScreen mainScreen].bounds.size.width
#define ScreenHeight     [UIScreen mainScreen].bounds.size.height


@interface JSQShowZoomImageScrollView ()

@property (nonatomic, copy) NSString *identifier;

@end

@implementation JSQShowZoomImageScrollView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.delegate = self;

        self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        self.bouncesZoom = YES;
        self.minimumZoomScale = 1;
        self.maximumZoomScale = 3;

        [self initImageView];
    }

    return self;
}

- (void)initImageView {

    _imageView = [[UIImageView alloc] init];

    // The imageView can be zoomed largest size
    _imageView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    _imageView.contentMode = UIViewContentModeScaleAspectFit;


    [self addSubview:_imageView];
    //点击退出图片浏览
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissViewController)];
    [tap setNumberOfTapsRequired:1];
    [self addGestureRecognizer:tap];

    // Add gesture,double tap zoom imageView.
    UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                       action:@selector(handleDoubleTap:)];
    [doubleTapGesture setNumberOfTapsRequired:2];
    [self addGestureRecognizer:doubleTapGesture];

    [tap requireGestureRecognizerToFail:doubleTapGesture];

}

- (instancetype)initWithImageReuseIdentifier:(NSString *)reuseIdentifier {
    self = [self init];
    if (reuseIdentifier) {
        self.identifier = reuseIdentifier;
    }
    return self;
}

#pragma mark - Zoom methods

- (void)handleDoubleTap:(UIGestureRecognizer *)gesture {
    float newScale = (self.zoomScale < self.maximumZoomScale) ? self.maximumZoomScale : self.minimumZoomScale;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gesture locationInView:gesture.view]];
    [self zoomToRect:zoomRect animated:YES];
}

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
    CGRect zoomRect;
    zoomRect.size.height = self.frame.size.height / scale;
    zoomRect.size.width = self.frame.size.width  / scale;
    zoomRect.origin.x = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
    return zoomRect;
}

#pragma mark - UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return _imageView;
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {

}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {

}

- (void)resetImageView:(UIImageView *)view {
    UIImage *image = view.image;

    view.frame = CGRectMake(0, 0, image.size.width, image.size.height);
}

- (void)dismissViewController {
    [_parent dismissViewControllerAnimated:YES completion:nil];
}

@end
